package models

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/cy77cc/hioshop/global"
	"time"
)

type ShowSetting struct {
	Model
	Banner         uint8 `json:"banner" gorm:"type:tinyint unsigned;default:0"`
	Channel        int8  `json:"channel" gorm:"type:tinyint;default:0"`
	IndexBannerImg int8  `json:"index_banner_img" gorm:"type:tinyint;default:0"`
	Notice         int8  `json:"notice" gorm:"type:tinyint;default:0"`
}

func (s *ShowSetting) GetSettings() (err error) {
	ctx := context.Background()
	get := global.REDIS.Get(ctx, fmt.Sprintf("showSetting:%d", 1))
	if get.Err() != nil {
		err = global.DB.Model(&ShowSetting{}).Where("id=?", 1).Find(s).Error
		data, _ := json.Marshal(s)
		err = global.REDIS.Set(ctx, fmt.Sprintf("showSetting:%d", 1), data, time.Hour).Err()
		return
	}
	data, _ := get.Result()
	err = json.Unmarshal([]byte(data), &s)
	return
}
